package com.boyinet.imesh2json.fx.controller

import com.boyinet.imesh2json.model.ProductTypeModel
import com.boyinet.imesh2json.viewmodel.ProductTypesViewModel
import de.felixroske.jfxsupport.FXMLController
import javafx.fxml.FXML
import javafx.fxml.Initializable
import javafx.scene.control.TextField
import javafx.scene.layout.AnchorPane
import jfxtras.styles.jmetro.JMetro
import org.springframework.beans.factory.annotation.Autowired
import java.net.URL
import java.util.*

@FXMLController
class TypeDialogViewController : Initializable {


    @FXML
    lateinit var root: AnchorPane

    @FXML
    lateinit var code: TextField

    @FXML
    lateinit var value: TextField

    @Autowired
    lateinit var productTypesViewModel: ProductTypesViewModel


    override fun initialize(location: URL?, resources: ResourceBundle?) {
        val jMetro = JMetro()
        jMetro.parent = root

        val current: ProductTypeModel = productTypesViewModel.current

        code.textProperty().bindBidirectional(current.code)
        value.textProperty().bindBidirectional(current.value)
    }


    fun onSave() {
        if (productTypesViewModel.update) {
            productTypesViewModel.update()
        } else {
            productTypesViewModel.insert()
        }
        root.scene.window.hide()
    }

    fun onCancel() {
        root.scene.window.hide()
    }
}